<?php
/*
Plugin Name: WPMU set default user role for administrator of blogs
Plugin URI: http://ued.ipleiria.pt
Description: Change blog administrator role
Version: 0.2
Author: Diogo Serra
Author URI: http://ued.ipleiria.pt
/*  Copyright YEAR  PLUGIN_AUTHOR_NAME  (email : PLUGIN AUTHOR EMAIL)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; Version 2.5.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

	http://creativecommons.org/licenses/by-nc-sa/2.5/pt/deed.en_US
*/
 //Get stored options
$default_user_role_options = get_site_option("default_user_role_options");
//*** GUI FUNCTION: add menu item for plugin config to Admin Site page
function default_user_role_config() {
	get_currentuserinfo();
	if (!is_site_admin()) return false;
	add_submenu_page('wpmu-admin.php', 'Default User Role Options', 'Default User Role', 10, 'wpmu_default_user_role' ,'default_user_role_config_page');
}
	
//*** GUI FUNCTION: Show config form
function default_user_role_config_page() {
	global $default_user_role_options, $wp_roles;
	require_once('admin.php');

	// if submit was pressed, process config data
	if (!is_site_admin()) die(__('<p>You do not have permission to access this page.</p>'));
	if ( isset($_POST['submit'])) {
		// check user permissions
		if ( !current_user_can('manage_options') ) {
			die(__('Cheatin&#8217; uh?'));
		// if okay, store data
		} else {
			update_site_option('default_user_role_options', $_POST['default_user_role_options']);
			 ?>		 
		<div id="message" class="updated fade"><p><?php _e('Options saved.') ?></p></div>
<?php	
		
		$default_user_role_options = get_site_option("default_user_role_options");
	}	
	// if submit not pressed, display config options
	}


?>

<div class="wrap">
	<h2>Change user role of administrator of new blogs</h2>
	<form action="" method="post" id="default_user_role" style="margin: auto;">
	<table class="form-table">
		<tr class="form-field form-required">
			<th scope="row" valign="top"><label for="default_user_role_active"><?php _e('Active') ?></label></th>
			<td class='check-column'><input type='checkbox' name='default_user_role_options[active]' id="default_user_role_active" value="1" <?php if($default_user_role_options['active']=="1"){ echo ("checked='checked'");} ?> /><br />
	           <?php _e('This must be checked in order to the plugin works'); ?></td>
		</tr>
			<tr class="form-field">
				<th scope="row" valign="top"><label for="category_array_parent"><?php _e('Role:') ?></label></th>
				<td><select name="default_user_role_options[role]" id="default_user_role_new_role"><?php
				foreach($wp_roles->role_names as $role => $name) {
					$selected = '';
					if( $role == $default_user_role_options['role'] )
						$selected = 'selected="selected"';
					echo "<option {$selected} value=\"{$role}\">{$name}</option>";
				}
				?></select>
				</td>
			</tr>
		</table>
		<p class="submit">
			<input type="submit" name="submit" value="<?php _e('Update'); ?>" />
		</p>
		</form>

	<div class='clear'></div>
</div>
<?php
}

function default_user_role_set($blog_id)  {
   
	global $default_user_role_options, $wpdb;
	//Confirm that the plugin is active
	if(!empty($default_user_role_options['active'])&&$default_user_role_options['active']=="1"){
		if($blog_id != "" && $blog_id !="0"){
			$blogownerid = $wpdb->get_var("SELECT post_author FROM wp_" . $blog_id . "_posts");
			if ($blogownerid) {
				$user = new WP_User($blogownerid);
				$user->set_role($default_user_role_options['role']);
			 }
		}else{
			return new WP_Error('no-user-id','Check that a new blog was and a new user');
		}
	}
}
add_action('wpmu_new_blog', 'default_user_role_set');
add_action('admin_menu', 'default_user_role_config');
?>